﻿using System;
using System.IO;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Resources;

using CE.User;
using CE.Global;
using CE.Database;
using CE.InputOut;
using CE.CarInsurance;

public partial class carinsurance_renewal_renewal_notice_update : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            GetMonthName();
            LoadNClass();
            LoadBrokerProvider();
            GetCustomerName();
        }
    }

    private void GetMonthName()
    {
        string[] monthEnN = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
        string[] monthThN = { "ม.ค.", "ก.พ.", "ม.ค.", "ม.ย.", "พ.ค.", "มิ.ย.", "ก.ค.", "ส.ค.", "ก.ย.", "ต.ค.", "พ.ย.", "ธ.ค." };

        if (Convert.ToString(Session["Language"]) == "th-TH")
        {
            for (int i = 1; i <= 12; i++)
            {
                ddlCoverageMonth.Items.Add(new ListItem(monthThN[i - 1], i.ToString()));
            }
        }
        else
        {
            for (int i = 1; i <= 12; i++)
            {
                ddlCoverageMonth.Items.Add(new ListItem(monthEnN[i - 1], i.ToString()));
            }
        }

        ddlCoverageMonth.Items.Insert(0, new ListItem(Resources.General.Select, "-1"));
    }

    private void LoadNClass()
    {
        ddlInsuranceType.Items.Clear();
        ddlInsuranceType.Items.Add(new ListItem(Resources.General.Select, "-1"));
        ddlInsuranceType.Items.Add(new ListItem(Resources.CarInsurance.Insurance_ClassOne, "10"));
        ddlInsuranceType.Items.Add(new ListItem(Resources.CarInsurance.Insurance_ClassOneLowCost, "11"));
        ddlInsuranceType.Items.Add(new ListItem(Resources.CarInsurance.Insurance_ClassTwoPlus, "21"));
        ddlInsuranceType.Items.Add(new ListItem(Resources.CarInsurance.Insurance_ClassTwo, "20"));
        ddlInsuranceType.Items.Add(new ListItem(Resources.CarInsurance.Insurance_ClassThreePlus, "31"));
        ddlInsuranceType.Items.Add(new ListItem(Resources.CarInsurance.Insurance_ClassThree, "30"));
    }

    private void LoadBrokerProvider()
    {
        DataTable _dtBrokerProvider = General.GetComboItem("BrokerProvider", true);
        ddlBrokerProvider.DataSource = _dtBrokerProvider;
        ddlBrokerProvider.DataTextField = _dtBrokerProvider.Columns[0].ColumnName.ToString();
        ddlBrokerProvider.DataValueField = _dtBrokerProvider.Columns[1].ColumnName.ToString();
        ddlBrokerProvider.DataBind();

        ddlBrokerProvider.Items.Insert(0, new ListItem("Select", "-1"));
    }

    private void GetCustomerName()
    {
        MyDatabase _db = new MyDatabase();
        object _name = _db.ExecuteScalarSqlStatement("SELECT TOP 1 name FROM tblNonGGCarMoreDetails where RefCEUserId = " +
            Request.QueryString["uid"] + " AND cecarid = " + Request.QueryString["cid"] + " ORDER BY modifieddate desc");
        if (_name != null)
        {
            lblCustName.Text = Convert.ToString(_name);
        }

        DataTable _dt = _db.GetDataTable("SELECT Premium, CarLicense, ProviderId, Class, QuoteRefNo, RenewalMonth, Remark, NotLead " + 
            "FROM tblRenewal WHERE CustomerId = " + Request.QueryString["uid"] + " AND CarId = " + Request.QueryString["cid"] + " AND DATEADD(dd,210, CreatedDate) > dbo.fn_getUTCDateTime()"); //AgentId IS NULL

        if (_dt != null)
        {
            if (_dt.Rows.Count > 0)
            {
                DataRow _dr = _dt.Rows[0];
                txtNetPremium.Text = _dr["Premium"].ToString();
                txtLicensePlate.Text = _dr["CarLicense"].ToString();
                ddlBrokerProvider.SelectedIndex = ddlBrokerProvider.Items.IndexOf(ddlBrokerProvider.Items.FindByValue(_dr["ProviderId"].ToString()));
                ddlInsuranceType.SelectedIndex = ddlInsuranceType.Items.IndexOf(ddlInsuranceType.Items.FindByValue(_dr["Class"].ToString()));
                lblQuoteRefNo.Text = _dr["QuoteRefNo"].ToString();
                ddlCoverageMonth.SelectedIndex = ddlCoverageMonth.Items.IndexOf(ddlCoverageMonth.Items.FindByValue(_dr["RenewalMonth"].ToString()));
                txtRemark.Text = _dr["Remark"].ToString();
                if (_dr["NotLead"].ToString() == "True")
                    chkNotGenerateLead.Checked = true;
            }
        }

        if (lblQuoteRefNo.Text.Trim() == "") lblQuoteRefNo.Text = Request.QueryString["qno"];
    }

    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        MyDatabase _db = new MyDatabase();
        string _spName = "[sprocCXCRMNonGGRenewalInsertUpdate]";
        SortedList _sPara = new SortedList();

        _sPara.Add("@CarId", Request.QueryString["cid"]);
        _sPara.Add("@Premium", txtNetPremium.Text);
        _sPara.Add("@CarLicense", txtLicensePlate.Text);
        _sPara.Add("@ProviderId", ddlBrokerProvider.SelectedValue);
        _sPara.Add("@Class", ddlInsuranceType.SelectedValue);
        _sPara.Add("@CustomerId", Request.QueryString["uid"]);
        _sPara.Add("@QuoteRefNo", lblQuoteRefNo.Text);
        _sPara.Add("@RenewalMonth", ddlCoverageMonth.SelectedValue);
        _sPara.Add("@Remark", txtRemark.Text);
        if (chkNotGenerateLead.Checked)
            _sPara.Add("@NotLead", chkNotGenerateLead.Checked);
        long rtn = _db.ExecuteSP(_spName, _sPara);

        if (rtn == 1)
        {
            lblError.Text = "";
            lblSuccess.Text = Resources.General.SuccessfullyUpdated;
        }
        else
        {
            lblSuccess.Text = "";
            lblError.Text = Resources.General.FAIL;
        }
    }
}
